package com.k.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.ShoppingCartDetail;
import com.k.demo.service.ShoppingCartDetailService;
import com.k.demo.service.ShoppingCartService;

@RestController
@RequestMapping("/shoppingcartdetail")
public class ShoppingCartDetailController {

	@Autowired
	private ShoppingCartDetailService shoppingCartDetailService;
	@Autowired
	private ShoppingCartService shoppingCartService;
	
	/**
	 * 菜品加入购物车
	 * 
	 */
	@RequestMapping("/add")
	public Object add(@RequestBody ShoppingCartDetail shoppingCartDetail ,HttpServletRequest request) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser loginuser = (CustomerUser) request.getSession().getAttribute("loginUser");
		
		if (loginuser==null) {
			map.put("err", "您还没有登录");
		}else {
			map.put("loginUser", loginuser);
			//获取购物车id
			int shopping_cart_id=shoppingCartService.simSelect(loginuser.getUser_id()).getShopping_cart_id();
			shoppingCartDetail.setShopping_cart_id(shopping_cart_id);
			//判断是否有同类菜品
			ShoppingCartDetail scd = shoppingCartDetailService.selectByIdAndAttach(shopping_cart_id, shoppingCartDetail.getFood_id(), shoppingCartDetail.getDetail_attach());
			if(scd==null) {
				//为空插入
				map.put("msg", shoppingCartDetailService.insertShoppingCartDetail(shoppingCartDetail));
			}else {
				//已有更新
				shoppingCartDetail.setFood_amount(shoppingCartDetail.getFood_amount()+scd.getFood_amount());
				shoppingCartDetail.setTotal_cost(shoppingCartDetail.getTotal_cost()+scd.getTotal_cost());
				shoppingCartDetail.setShopping_cart_detail_id(scd.getShopping_cart_detail_id());
				map.put("msg", shoppingCartDetailService.updateShoppingCartDetail(shoppingCartDetail));
			}
		}
		return map;
	}
	
	/*
	 * 减少购物车的菜品
	 */
	@RequestMapping("/del")
	public Object del(@RequestBody ShoppingCartDetail shoppingCartDetail ,HttpServletRequest request) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser loginuser = (CustomerUser) request.getSession().getAttribute("loginUser");
		
		if (loginuser==null) {
			map.put("err", "您还没有登录");
		}else {
			map.put("loginUser", loginuser);
			//获取购物车id
			int shopping_cart_id=shoppingCartService.simSelect(loginuser.getUser_id()).getShopping_cart_id();
			shoppingCartDetail.setShopping_cart_id(shopping_cart_id);
			//判断是否有同类菜品
			ShoppingCartDetail scd = shoppingCartDetailService.selectByIdAndAttach(shopping_cart_id, shoppingCartDetail.getFood_id(), shoppingCartDetail.getDetail_attach());
			if(scd!=null) {
				//不为空则原来的减少现在的
				shoppingCartDetail.setFood_amount(scd.getFood_amount()-shoppingCartDetail.getFood_amount());
				shoppingCartDetail.setTotal_cost(scd.getTotal_cost()-shoppingCartDetail.getTotal_cost());
				shoppingCartDetail.setShopping_cart_detail_id(scd.getShopping_cart_detail_id());
				map.put("msg", shoppingCartDetailService.updateShoppingCartDetail(shoppingCartDetail));
			}
		}
		return map;
	}
	
	/*
	 * 删除购物车的菜品
	 */
	@RequestMapping("/remove")
	public Object remove(@RequestBody ShoppingCartDetail shoppingCartDetail ,HttpServletRequest request) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser loginuser = (CustomerUser) request.getSession().getAttribute("loginUser");
		
		if (loginuser==null) {
			map.put("err", "您还没有登录");
		}else {
			map.put("loginUser", loginuser);
			map.put("msg", shoppingCartDetailService.deleteShoppingDetail(shoppingCartDetail.getShopping_cart_detail_id()));
		}
		return map;
	}
	
	/*
	 * 另一种修改购物车明细的服务
	 * 
	 * */
	@RequestMapping("/exchange")
	public Object exchange(@RequestBody ShoppingCartDetail shoppingCartDetail,HttpSession session) {
		Map<String,Object> map=new HashMap<>();
		CustomerUser loginuser = (CustomerUser)session.getAttribute("loginUser");
		if (loginuser==null) {
			map.put("err", "您还没有登录");
		}else {
			//获取购物车id
			int shopping_cart_id=shoppingCartService.simSelect(loginuser.getUser_id()).getShopping_cart_id();
			shoppingCartDetail.setShopping_cart_id(shopping_cart_id);
			//判断是否有同类菜品
			ShoppingCartDetail scd = shoppingCartDetailService.selectByIdAndAttach(shopping_cart_id, shoppingCartDetail.getFood_id(), shoppingCartDetail.getDetail_attach());
			if(scd==null) {
				//为空插入
				map.put("msg", shoppingCartDetailService.insertShoppingCartDetail(shoppingCartDetail));
			}else {
				//已有更新
				scd.setFood_amount(shoppingCartDetail.getFood_amount());
				scd.setTotal_cost(shoppingCartDetail.getTotal_cost());
				map.put("msg", shoppingCartDetailService.updateShoppingCartDetail(scd));
			}
		}
		return map;
	}
	
	/*
	 * 另一种删除购物车明细的服务
	 */
	@RequestMapping("/exremove")
	public Object exremove(@RequestBody ShoppingCartDetail shoppingCartDetail ,HttpSession session) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser loginuser = (CustomerUser)session.getAttribute("loginUser");
		if (loginuser==null) {
			map.put("err", "您还没有登录");
		}else {
			//获取购物车id
			int shopping_cart_id=shoppingCartService.simSelect(loginuser.getUser_id()).getShopping_cart_id();
			shoppingCartDetail.setShopping_cart_id(shopping_cart_id);
			map.put("msg", shoppingCartDetailService.deleteDetail(shoppingCartDetail));
		}
		return map;
	}
	/*
	 * 批量删除购物车的菜品
	 */
	@RequestMapping("/removeIn")
	public Object removeIn(@RequestBody List<Integer> list ,HttpSession session) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser loginuser = (CustomerUser) session.getAttribute("loginUser");

		
		if (loginuser==null) {
			map.put("err", "您还没有登录");
		}else {
			map.put("msg", shoppingCartDetailService.deleteShoppingDetailIn(list));
		}
		return map;
	}
}
